package com.j3l11234.myy_sign.controller;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.j3l11234.myy_sign.entity.UserEntity;
import com.j3l11234.myy_sign.model.SettingModel;
import com.j3l11234.myy_sign.model.UserModel;
import com.j3l11234.myy_sign.util.MyException;
import com.j3l11234.myy_sign.util.PrivilegeCheck;



@Controller
@RequestMapping("/admin")
public class AdminController {
	@Autowired
	private SessionFactory sessionFactory;
	@Autowired
	private SettingModel settingModel;
	@Autowired
	private UserModel userModel;
	
	@RequestMapping("/index")
	@PrivilegeCheck(privilege = UserEntity.Admin, needLogin = true)
    public Object index(ModelMap model) {
		model.addAttribute("navbar_item", "admin");
		model.addAttribute("sidebar_item", "index");
		
		String systemInfo = System.getProperty("os.name")+" / "+System.getProperty("os.arch")+" / "+System.getProperty("os.version");
		String jvmInfo = System.getProperty("java.vm.name")+" / "+System.getProperty("java.specification.version");
		String dbVersion="";
		Session session = null;
		try {
			session = sessionFactory.openSession();
			Query query = session.createSQLQuery("SELECT VERSION()");
			dbVersion = query.uniqueResult().toString();  
		} catch (Exception e) {} finally{
			if(session != null){
				session.close();
			}
		}
		
		model.addAttribute("systemInfo", systemInfo);
		model.addAttribute("jvmInfo", jvmInfo);
		model.addAttribute("dbVersion", dbVersion);
    
        return "admin/index";
    }
	
	@RequestMapping("/board")
	@PrivilegeCheck(privilege = UserEntity.Admin, needLogin = true)
    public Object board(ModelMap model) {
		model.addAttribute("navbar_item", "board");
		model.addAttribute("sidebar_item", "board");
        
		try {
			model.addAttribute("board_text", settingModel.getBoardText());
		} catch (MyException e) {
			e.printStackTrace();
		}
        return "admin/board";
    }
	
	@RequestMapping(value="/board",  method = RequestMethod.POST)
	@PrivilegeCheck(privilege = UserEntity.Admin, needLogin = true)
    public Object postBoard(
    		@RequestParam(value="board_text", required = true) String boardText,
    		ModelMap model) {
		try {
			settingModel.setBoardText(boardText);
			model.addAttribute("postResult", true);
		} catch (MyException e) {
			model.addAttribute("postResult", false);
			e.printStackTrace();
		}
		return board(model);
    }
	
	
	@RequestMapping("/check")
	@PrivilegeCheck(privilege = UserEntity.Admin, needLogin = true)
    public Object check(ModelMap model) {
		model.addAttribute("navbar_item", "check");
		model.addAttribute("sidebar_item", "check");
        
		SimpleDateFormat sdf=new SimpleDateFormat("HH:mm:ss");
		try {
			model.addAttribute("signin_time", sdf.format(settingModel.getSigninTime()));
			model.addAttribute("signout_time", sdf.format(settingModel.getSignoutTime()));
		} catch (MyException e) {
			e.printStackTrace();
		}
		
        return "admin/check";
    }
	
	@RequestMapping(value="/check",  method = RequestMethod.POST)
	@PrivilegeCheck(privilege = UserEntity.Admin, needLogin = true)
    public Object postCheck(
    		@RequestParam(value="in_time", required = true) String strInTime,
    		@RequestParam(value="out_time", required = true) String strOutTime,
    		ModelMap model) {
		
		DateFormat timeFormat = new SimpleDateFormat("HH:mm:ss");
		Date inTime = null, outTime = null;
        try {
        	inTime = timeFormat.parse(strInTime);
        	outTime = timeFormat.parse(strOutTime);
        	settingModel.setSigninTime(inTime);
			settingModel.setSignoutTime(outTime);
			
			model.addAttribute("postResult", true);
		} catch (Exception e) {
			model.addAttribute("postResult", false);
			e.printStackTrace();
		}

        return check(model);
    }
	
	@RequestMapping("/user")
	@PrivilegeCheck(privilege = UserEntity.Admin, needLogin = true)
	public Object user(ModelMap model) {
		model.addAttribute("navbar_item", "admin");
		model.addAttribute("sidebar_item", "user");
		
		try {
			List<UserEntity> userList = userModel.getUser();
			model.addAttribute("userList", userList);
		} catch (MyException e) {
			e.printStackTrace();
		}
		
        return "admin/user";
    }
	
	@RequestMapping("/log")
	@PrivilegeCheck(privilege = UserEntity.Admin, needLogin = true)
    public Object log(ModelMap model) {
		model.addAttribute("navbar_item", "admin");
		model.addAttribute("sidebar_item", "log");
		
        return "admin/log";
    }
}
